#include "SavingsAccount.hh"
#include "CheckingAccount.hh"
#include "Transaction.hh"
void SavingsAccount::deposit(double amount, const std::string& note) {
    if (amount > 0) {
        balance += amount;
        Transaction transaction(amount, "Deposit", note);
        recordTransaction(transaction);
    } else {
        std::cout << "Invalid deposit amount.\n";
    }
}
void SavingsAccount::withdraw(double amount, const std::string& note) {
    if (amount > 0 && balance >= amount) {
        balance -= amount;
        Transaction transaction(-amount, "Withdrawal", note);
        recordTransaction(transaction);
    } else {
        std::cout << "Insufficient funds or invalid withdrawal amount.\n";
    }
}
void SavingsAccount::transferToChecking(CheckingAccount& to, double amount, const std::string& note) {
    if (to.getAccountID() != this->getCheckingID(to))
    {
        std::cout << "Invalid transfer account.\n";
        return;
    }
    
    if (amount > 0 && balance >= amount) {
        balance -= amount;
        to.deposit(amount, note);
        Transaction transaction(-amount, "Transfer to Checking", note);
        recordTransaction(transaction);
    } else {
        std::cout << "Insufficient funds or invalid transfer amount.\n";
    }
}

std::string SavingsAccount::getCheckingID(CheckingAccount& to) {
    return to.getAccountID();
}

SavingsAccount::SavingsAccount(const std::string& id, double initialBalance, const std::string& owner, Bank* bank)
    : Account(id, initialBalance, owner), bank(bank) {}

void SavingsAccount::setBank(Bank* bank) {
    this->bank = bank;
}
void SavingsAccount::accessAccount() {
    std::cout << "Savings Account " << accountID << ": $" << balance << "\n";
    std::cout << "Owner: " << owner << "\n";
    std::cout << "Transactions:\n";
    for (const Transaction& transaction : transactions) {
        transaction.print();
    }
    std::cout << "---------------------------------------------------\n";
    std::cout << "Options:\n";
    std::cout<<"1.Transfer to Checking Account\n";
    std::cout << "2. View transaction history\n";
    std::cout << "3. Generate bank account report\n";
    std::cout << "4. Return to customer page\n";
    std::cout << "---------------------------------------------------\n";
    int choice1;
    std::cout << "Please choose: ";
    std::cin >> choice1;
    if (std::cin.fail()) {
        std::cin.clear();
        std::cin.ignore(100, '\n');
        std::cout << "Invalid input. Please enter a number.\n";
    }
    if (choice1 < 1 || choice1 > 4) {
        std::cin.clear();
        std::cin.ignore(100, '\n');
        std::cout << "Invalid choice. Please try again.\n";
    }
    double amount; 
std::string toAccountID, note;
Customer* toCustomer = nullptr;  // 外部声明
Account* targetAccount = nullptr; // 外部声明
    switch (choice1) {
        case 1: 
            std::cout << "Enter the amount to transfer: ";
            std::cin >> amount;
            if (std::cin.fail()) {
                std::cin.clear();
                std::cin.ignore(100, '\n');
                std::cout << "Invalid input. Please enter a number.\n";
            }
            if (amount < 0) {
                std::cin.clear();
                std::cin.ignore(100, '\n');
                std::cout << "Invalid amount. Please try again.\n";
            }
            std::cout << "Enter the note for the transaction: ";
            std::cin.ignore();
            std::getline(std::cin, note);
            toCustomer = bank->findCustomer(owner); 
            if (toCustomer) {
                targetAccount = toCustomer->getCheckingAccount(); 
                if (targetAccount) {
                    transferToChecking(*dynamic_cast<CheckingAccount*>(targetAccount), amount, note);
                } else {
                    std::cout << "No checking account found for this customer.\n";
                }
            } else {
                std::cout << "Customer not found.\n";
            }
            break;
        case 2: 
            for (const Transaction& transaction : transactions) {
                transaction.print();
            }
            break;
        case 3:
            bank->generateAccountReport(accountID);
            break;
        case 4: 
            break;
    }



}